package likou.offer;

import java.util.Arrays;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/31  19:54
 */
public class _57 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum(new int[]{10,26,30,31,47,60},40)));
    }
    public static int[] twoSum(int[] nums, int target) {
        //查找极限的下标
        int maxIndex = erfen(nums,target);
        int[] daan = new int[2];
        int i=0,j=maxIndex;
        while (i<j){
            if (nums[i]+nums[j]>target){
                j--;
            }
            if (nums[i]+nums[j]<target){
                i++;
            }
            if (nums[i]+nums[j]==target){
                daan[0] = nums[i];
                daan[1] = nums[j];
                break;
            }
        }

        return daan;
    }
    public static int erfen(int[] nums , int target){
        int low = 0;
        int high = nums.length-1;
        while (high>low){
            int now = (high+low)/2;
            if (nums[now]>target){
                high = now-1;
                continue;
            }
            if (nums[now]<target){
                low = now+1;
                continue;
            }
            if (nums[now]==target){
                return now;
            }
        }
        return high;
    }
}
